package com.app.couponsharelife.ui.adapter

import androidx.databinding.DataBindingUtil
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.UserTagBean
import com.app.couponsharelife.databinding.UserLabelListItemItemBinding

class UserLabelListItemAdapter(private val list: List<UserTagBean.SonsBean>) :
        androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        return ViewHolder(DataBindingUtil.inflate(
                LayoutInflater.from(parent.context), R.layout.user_label_list_item_item, parent, false))
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
        val sonsBean = list[position]
        (holder as ViewHolder).databinding.tvLabel.text = sonsBean.name
        if (sonsBean.checked == null) {
            holder.databinding.tvLabel.setTextColor(ContextCompat.getColor(holder.itemView.context, R.color.act_login_bot_text_color))
            holder.databinding.tvLabel.setBackgroundResource(R.drawable.act_label_no_select_bg)
        } else {
            if (sonsBean.checked!!) {
                holder.databinding.tvLabel.setTextColor(ContextCompat.getColor(holder.itemView.context, R.color.fra_order_item_price_text_color))
                holder.databinding.tvLabel.setBackgroundResource(R.drawable.act_label_select_bg)
            } else {
                holder.databinding.tvLabel.setTextColor(ContextCompat.getColor(holder.itemView.context, R.color.act_login_bot_text_color))
                holder.databinding.tvLabel.setBackgroundResource(R.drawable.act_label_no_select_bg)
            }
        }
    }

    inner class ViewHolder(val databinding: UserLabelListItemItemBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(databinding.root) {
        init {
            databinding.root.setOnClickListener {
                if (adapterPosition != -1) {
                    if (list[adapterPosition].checked == null) {
                        list[adapterPosition].checked = true
                    } else {
                        list[adapterPosition].checked = !list[adapterPosition].checked!!
                    }
                    notifyItemChanged(adapterPosition)
                }
            }
        }
    }
}